﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBConnectionLib;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    [Ignore]
    public class TestDatabaseProject
    {

        [Test]
        public void TestProjectNameConstructor()
        {
            string strFileName = BaseDirectoryHelper.TestFiles_control_DatabaseProject + "\\TestDatabaseProject.xml";
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                BaseDirectoryHelper.TestFiles_control_DatabaseProject, "TestDatabaseProject");
            Assert.AreEqual(strFileName, oDBProject.ProjectFileName);
            Assert.AreEqual("TestDatabaseProject", oDBProject.ProjectName);
            Assert.AreEqual(BaseDirectoryHelper.TestFiles_control_DatabaseProject, oDBProject.BaseDirectory);
        }
        [Test]
        public void TestFileNameConstructor()
        {
            string strFileName = BaseDirectoryHelper.TestFiles_control_DatabaseProject + "\\TestDatabaseProject.xml";
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(strFileName);
            Assert.AreEqual(strFileName, oDBProject.ProjectFileName);
            Assert.AreEqual("TestDatabaseProject", oDBProject.ProjectName);
            Assert.AreEqual(BaseDirectoryHelper.TestFiles_control_DatabaseProject, oDBProject.BaseDirectory);
            Assert.IsNull(oDBProject.DefaultDatabaseName);
            Assert.Less(oDBProject.SourceDatabases.Count,1);
        }

        [Test]
        public void TestLoadInvalidFileName()
        {
            string strFileName = BaseDirectoryHelper.TestFiles_control_DatabaseProject + "\\Invalid.xml";
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(strFileName);
            try
            {
                oDBProject.LoadFromDisk(false);
                Assert.Fail(" Should throw and exception if " + strFileName + " does not exist");
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("Cannot load Database Project file"))
                {
                    Assert.Fail("Did not throw the right exception");
                }
            }
            
        }
        [Test]
        public void TestDefaultDatabaseName()               
        {
            string strFileName = BaseDirectoryHelper.TestFiles_control_DatabaseProject + "\\TestDatabaseProject.xml";
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(strFileName);
            oDBProject.LoadFromDisk(false);
            Assert.AreEqual("DBSourceToolsTestDB",(string)oDBProject.DefaultDatabaseName);
        }

        [Test]
        public void TestLoadSourceDatabases()
        {
            string strFileName = BaseDirectoryHelper.TestFiles_control_DatabaseProject + "\\TestDatabaseProject.xml";
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(strFileName);
            oDBProject.LoadFromDisk(false);
            Assert.Greater(oDBProject.SourceDatabases.Count,0);
        }

        [Test]
        public void TestLoadTargetDatabases()
        {
            string strFileName = BaseDirectoryHelper.TestFiles_control_DatabaseProject + "\\TestDatabaseProject.xml";
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(strFileName);
            oDBProject.LoadFromDisk(false);
            Assert.Greater(oDBProject.SourceDatabases.Count, 0);
            DBSourceToolsLib.SourceDatabase oSourceDatabase = oDBProject.SourceDatabases[0];
            Assert.Greater(oSourceDatabase.TargetDatabases.Count, 0);
        }

        [Test]
        public void TestAddNewDatabase()
        {
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                BaseDirectoryHelper.TestFiles_control_DatabaseProject, "TestDatabaseProject");
            Assert.AreEqual(0, oDBProject.SourceDatabases.Count);

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDatabaseObject());

            DBSourceToolsLib.SourceDatabase oSource = oDBProject.SourceDatabases[0];
            Assert.AreEqual(BaseDirectoryHelper.TestFiles_control_DatabaseProject + "\\" + oStub.GetDatabaseObject().NickName,
                oSource.BaseDirectory);

        }

        [Test]
        public void TestResetBaseDirectory()
        {
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                BaseDirectoryHelper.TestFiles_control_DatabaseProject, "TestDatabaseProject");
            Assert.AreEqual(0, oDBProject.SourceDatabases.Count);

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDatabaseObject());

            DBSourceToolsLib.SourceDatabase oSource = oDBProject.SourceDatabases[0];
            Assert.AreEqual(BaseDirectoryHelper.TestFiles_control_DatabaseProject + "\\" + oStub.GetDatabaseObject().NickName,
                oSource.BaseDirectory);

            oDBProject.BaseDirectory = "test";
            //DBSourceToolsLib.SourceDatabase oSource = oDBProject.SourceDatabases[0];
            Assert.AreEqual("test\\" + oStub.GetDatabaseObject().NickName,
                oSource.BaseDirectory);

        }

        [Test]
        public void TestCreateDirectories()
        {
            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(BaseDirectoryHelper.TestFiles_output);
            string strExpectedDirectory = BaseDirectoryHelper.TestFiles_output +
                "\\TestDatabaseProject";

            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                strExpectedDirectory, "TestDatabaseProject");

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDatabaseObject());

            oDBProject.CreateBaseDirectories();

            Assert.IsTrue(System.IO.Directory.Exists(strExpectedDirectory), strExpectedDirectory + " not created");
            foreach (DBSourceToolsLib.SourceDatabase oSource in oDBProject.SourceDatabases)
            {
                string strSourceDir = strExpectedDirectory + "\\" + oSource.NickName;
                Assert.IsTrue(System.IO.Directory.Exists(strSourceDir), strSourceDir + " not created");
            }

        }

        [Test]
        public void TestWriteProjectFile()
        {
            FileHelper oFileHelper = new FileHelper();
            string strExpectedDirectory = BaseDirectoryHelper.TestFiles_output +
                "\\WriteProjectFile";
            string strControlFile = BaseDirectoryHelper.TestFiles_control_DatabaseProject + "\\TestDatabaseProject_write.xml";
            oFileHelper.DeleteFilesInDir(strExpectedDirectory);

            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                strExpectedDirectory, "TestDatabaseProject");

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDatabaseObject());

            oDBProject.CreateBaseDirectories();

            Assert.IsTrue(System.IO.Directory.Exists(strExpectedDirectory), strExpectedDirectory + " not created");

            oDBProject.WriteProjectFile();
            oFileHelper.CompareFiles(strControlFile, oDBProject.ProjectFileName);
            //foreach (DBSourceToolsLib.SourceDatabase oSource in oDBProject.SourceDatabases)
            //{
            //    string strSourceDir = strExpectedDirectory + "\\" + oSource.NickName;
            //    Assert.IsTrue(System.IO.Directory.Exists(strSourceDir), strSourceDir + " not created");
            //}

        }

        [Test]
        public void TestWriteProjectFile_TargetDatabases()
        {
            FileHelper oFileHelper = new FileHelper();
            string strExpectedDirectory = BaseDirectoryHelper.TestFiles_output +
                "\\WriteProjectFile";
            string strControlFile = BaseDirectoryHelper.TestFiles_control_DatabaseProject + 
                "\\TestDatabaseProject_write_Targets.xml";
            oFileHelper.DeleteFilesInDir(strExpectedDirectory);

            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                strExpectedDirectory, "TestDatabaseProject");

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDatabaseObject());
            oDBProject.SourceDatabases[0].AddTargetDatabase(oStub.GetDBSourceToolsTestDB(), false);

            oDBProject.CreateBaseDirectories();

            Assert.IsTrue(System.IO.Directory.Exists(strExpectedDirectory), strExpectedDirectory + " not created");

            oDBProject.WriteProjectFile();
            oFileHelper.CompareFiles(strControlFile, oDBProject.ProjectFileName);

        }

        [Test]
        public void TestGetDatabaseEx()
        {
            string strProjectFile = BaseDirectoryHelper.TestFiles_control + "\\DBSourceToolsTestDB.xml";
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(strProjectFile);
            oDBProject.LoadFromDisk(false);

            Assert.IsNotNull( oDBProject.GetDatabaseEx("DBSourceToolsTestDB"));
            Assert.IsNotNull( oDBProject.GetDatabaseEx("DeployDBSourceToolsTestDB"));
        }

    }
}
